<template>
	<view class="">
		<Navbar></Navbar>
		<view class="u-m-30 u-radius-10 u-back-white u-p-t-20 u-p-l-28 u-p-b-28 u-p-r-28 u-m-b-34">
			<view class="u-font-28 u-flex-col u-m-b-58">
				<view class="u-flex">
					<u-icon size="36" name="dian1" color="#3070F6FF" custom-prefix="custom-icon" />
					<text> 对接人作为解决人：</text>
				</view>
				<ContactsListItem ref="contactChild" @handleCheckUser="handleCheckUser" :lists="lists" :hideChangeCard="true" />
			</view>
			<u-radio-group v-model="checked" @change="handleChangeRadio">
				<view class="u-flex u-row-between">
					<view class="">
						<u-icon size="36" name="dian1" color="#3070F6FF" custom-prefix="custom-icon" />
						<text>通过其他渠道解决</text>
					</view>
					<u-radio name="0" shape="circle" />
				</view>
			</u-radio-group>
		</view>
		<view style="height: 200rpx" />
		<Bottom @confirm="confirm"></Bottom>
	</view>
</template>

<script>
	import {
		getSupplyNameById,
		solveSupply,
		UpdateSupplyRecord,
		updateSupplySolveStatus
	} from "@/network/demand.js"
	import Navbar from '@/components/Navbar.vue'
	import Bottom from '@/components/Bottom.vue'
	import ContactsListItem from '../components/ContactsListItem.vue'
	export default {
		components: {
			Navbar,
			Bottom,
			ContactsListItem
		},
		data() {
			return {
				background: {
					background: "transparent"
				},
				checked: "",
				lists: [],
				demandId: "",
				selectedUserId: "",
				selectedRecordId: ""
			}
		},
		onLoad(options) {
			this.demandId = options.id
			this.handleGetName(options.id)
		},
		methods: {
			handleCheckUser(users) {
				this.checked = ""
				this.selectedUserId = users.user_id
				this.selectedRecordId = users.id
			},
			handleGetName(id) {
				getSupplyNameById({
					demand_id: id
				}).then(res => {
					this.lists = res.data
				})
			},
			handleChangeRadio(e) {
				this.checked = "0"
				this.selectedUserId = ""
				this.selectedRecordId = ""
				this.$refs.contactChild.changeCheckValue()
			},
			confirm() {
				if (this.checked == "0") {
					solveSupply({
						channel_id: 4,
						demand_id: this.demandId
					}).then(() => {
						uni.showToast({
							title: "需求已解决",
							icon: "none"
						})
						uni.switchTab({
							url: "/pages/news/news?tabCurrent=1"
						})
					})
				} else if (this.selectedUserId) {
					UpdateSupplyRecord({
						user_id: uni.getStorageSync("user_id"),
						status: 1,
						id: this.selectedRecordId
					}).then(() => {
						updateSupplySolveStatus({ demand_id: this.demandId, solve_user_id: this.selectedUserId }).then(() => {
							uni.showToast({
								title: "需求已解决",
								icon: "none"
							})
							uni.switchTab({
								url: "/pages/news/news?tabCurrent=1"
							})
						})
					})
				} else {
					uni.showToast({
						title: "请选择解决方式",
						icon: "none"
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	::v-deep .u-radio {
		display: block !important;
	}
</style>
